package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.pro.Workflow;
import com.qidianit.mes.pro.mapper.ProcessMapper;
import com.qidianit.mes.pro.mapper.WorkflowMapper;

import com.qidianit.mes.pro.mapper.WorkflowProcessMapper;
import com.qidianit.mes.pro.mapper.WorkflowProductMapper;
import com.qidianit.mes.pro.service.WorkflowService;
import com.qidianit.mes.sys.mapper.WorkstationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qidianit.mes.pojo.pro.Process;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: wang fei long
 * @Date: 2025/9/17 0017 - 16:10
 */
@Service
public class WorkflowServiceImpl implements WorkflowService {

    @Autowired
    private WorkflowMapper workflowMapper;
    @Autowired
    private WorkflowProductMapper workflowProductMapper;
    @Autowired
    private WorkflowProcessMapper workflowProcessMapper;
    @Autowired
    private ProcessMapper ProcessMapper;
    @Autowired
    private WorkstationMapper workstationMapper;
    @Override
    public Result<PageInfo<Workflow>> selectList(Workflow workflow, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        ArrayList<Workflow> workflows = workflowMapper.selectList(workflow);
        return Result.success(new PageInfo<>(workflows));
    }

    @Override
    public Result<Workflow> selectByWorkflowCode(String workflowCode) {
        return Result.success(workflowMapper.selectByWorkflowCode(workflowCode));
    }

    @Override
    public Result insertWorkflow(Workflow workflow) {
        if (workflow.getWorkflowCode() == null || "".equals( workflow.getWorkflowCode()) ||
                workflow.getWorkflowName() == null ||"".equals( workflow.getWorkflowName()) ||
                workflow.getStatus() == null ||
                workflow.getWorkflowDescription() == null || "".equals( workflow.getWorkflowDescription()) ||
                workflow.getRemarks() == null || "".equals( workflow.getRemarks())
        ){
            return Result.error("添加失败，输入信息不完整");
        }
        Workflow workflow1 = workflowMapper.selectByWorkflowCode(workflow.getWorkflowCode());
        if (workflow1 != null){
            return Result.error("修改失败，工艺有重复");
        }
        return Result.success(workflowMapper.insertWorkflow(workflow));
    }

    @Override
    public Result updateById(Workflow workflow) {
        if (workflow.getWorkflowName() == null ||"".equals( workflow.getWorkflowName()) ||
                workflow.getStatus() == null ||
                workflow.getWorkflowDescription() == null || "".equals( workflow.getWorkflowDescription()) ||
                workflow.getRemarks() == null || "".equals( workflow.getRemarks())
        ){
            return Result.error("修改失败，修改信息不完整");
        }
        Workflow workflow1 = workflowMapper.selectByWorkflowCode(workflow.getWorkflowCode());
        if (workflow1 != null && workflow1.getId() != workflow.getId()){
            return Result.error("修改失败，工艺有重复");
        }
        return workflowMapper.updateId(workflow)==1?Result.success():Result.error("操作失败");
    }

    @Override
    public Result deleteById(Long id) {
        return workflowMapper.deleteById(id)==1?Result.success():Result.error("操作失败");
    }

    @Override
    public Result batchDelete(Long[] ids) {
        return workflowMapper.batchDelete(ids) > 0?Result.success():Result.error("操作失败");
    }



    @Override
    public Result<ArrayList<Workflow>> selectWorkflowById(Long[] ids) {
//        workflowProductMapper.selectByProductCode()
        return null;
    }

    @Override
    public Result<ArrayList<Process>> selectProcessByWorkflowId(Long id) {
        Long[] longs = workflowProcessMapper.selectProcessIdByWorkflowId(id);
        ArrayList<Process> processes = ProcessMapper.selectByIds(longs);
        return Result.success(processes);
    }
}
